package net.tv90.bfmccore.api.info;

import net.tv90.bfmccore.game.GameBase;
import net.tv90.bfmccore.game.GameManager;
import net.tv90.bfmccore.helper.FactionHelper;
import net.tv90.bfmccore.util.TimeUtil;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class ServerInfo {

    public static String getServerName() {
        return "BFMC";
    }

    public static String getServerVersion() {
        return "2025暑假更新";
    }

    public static String getServerIP() {
        return "game.bfmc.top";
    }

    public static final List<String> blackListPlayers = new ArrayList<>();
//            new ArrayList<>
//            (Arrays.asList("ddd233", "FNT_FQJ", "Komeiji_Koishi", "BIU_wuzhu",
//            "hyabcccc", "Paramond", "Thecool_winter"));

    public static String randomFaction() {
        String faction1 = GameManager.getInstance().currentGame.firstFactionName;
        String faction2 = GameManager.getInstance().currentGame.secondFactionName;
        boolean distribution = new Random().nextBoolean();
        if (distribution) {
            return faction1;
        } else {
            return faction2;
        }
    }

    public static String getRandomOfflinePlayerName() {
        OfflinePlayer[] offlinePlayers = Bukkit.getOfflinePlayers();
        Random random = new Random();
        if (offlinePlayers.length > 0) {
            OfflinePlayer randomPlayer = offlinePlayers[random.nextInt(offlinePlayers.length)];
            return randomPlayer.getName();
        }
        return null;
    }

    public static String getPlayerList() {
        StringBuilder sb = new StringBuilder();
        int playerNum = Bukkit.getOnlinePlayers().size();
        if (playerNum == 0) {
//            int num = 5 + new Random().nextInt(5);
//            sb.append("当前").append(num).append("名玩家在线\n");
//            for (int i = 0; i < num; i++) {
//                sb.append("[").append(randomFaction()).append("]").append(getRandomOfflinePlayerName()).append("\n");
//            }
//            return sb.toString();
            return "服务器当前没有玩家在线~QAQ\n挂机暖服可获得在线奖励哦~";
        } else {
            List<Player> playerList = Bukkit.getOnlinePlayers()
                    .stream()
                    .filter((e) -> !blackListPlayers.contains(e.getName()))
                    .collect(Collectors.toList());
            sb.append("当前").append(playerList.size()).append("名玩家在线\n");
            for (Player player : playerList) {
                if(player.isOp()) {
                    sb.append("[管理员]").append(player.getName()).append("\n");
                } else {
                    sb.append("[").append(FactionHelper.getCountry2(player)).append("]").append(player.getName()).append("\n");
                }
            }
        }
        return sb.toString();
    }

    public static String getMapInfo() {
        GameBase gameBase = GameManager.getInstance().currentGame;
        return "当前地图: \n{map}-{gamemode}({faction1} {ticket1}-{ticket2} {faction2})\n本场对局最晚将于{time}后结束"
                .replace("{map}", gameBase.getMapName_Lang())
                .replace("{gamemode}", gameBase.gameMode)
                .replace("{faction1}", gameBase.firstFactionName)
                .replace("{ticket1}", String.valueOf(gameBase.firstFactionTicket))
                .replace("{ticket2}", String.valueOf(gameBase.secondFactionTicket))
                .replace("{faction2}", gameBase.secondFactionName)
                .replace("{time}", TimeUtil.secondsToDate(gameBase.currentTime));
    }

    public static String getServerInfo() {
        return getServerName() + "_" + getServerVersion() + '\n' + getMapInfo() + '\n' + getPlayerList();
    }

    public static String getCurrentMapPool() {
        StringBuilder sb = new StringBuilder("当前地图池：\n");
        for (GameBase gameBase : GameManager.getInstance().currentGamePool) {
            sb.append(gameBase.mapName_Lang).append('-').append(gameBase.gameType.name_lang).append("\n");
        }
        return sb.toString();
    }
}
